﻿/*---------------------------------------------------------------- 
// Copyright 2005-2016 My Tech. Co. Ltd. All Rights Reserved.
// 版权所有。  
// 
// 文件名： CradleHeadBll
// 文件功能描述： 
// 
//  
// 创建标识： wx2016/11/11 16:38:35
// 
// 修改标识： 
// 修改描述： 
//----------------------------------------------------------------*/

using MyBLL.Dto;
using MyMiddleware;
using MyMiddleware.ComDev;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyBLL
{
    public class MyComBll
    {
        private static Device device;

        //线程安全的接收消息队列
        public static ConcurrentQueue<List<TargetInfo>> getComQueue;

        public static ConcurrentDictionary<string, TargetInfo> MyComDataDic;

        /// <summary>
        /// 获取校验和，出去命令头两个字节的累加和计算
        /// </summary>
        /// <param name="comand">指令</param>
        /// <returns></returns>
        private static byte[] checkComand(byte[] comand)
        {
            byte checksum = 0;
            for (int i = 2; i < comand.Length -1; i++)
            {
                checksum += comand[i];
            }
            comand[comand.Length - 1] = checksum;
            return comand;
        }

        

        /// <summary>
        /// 发送指令
        /// </summary>
        /// <param name="cmd">指令类型</param>
        /// <param name="device">设备</param>
        /// <returns></returns>
        public static bool GetData(Device device)
        {
            device.OnGetData += device_OnGetData;

            return true;
        }

        static void device_OnGetData(object sender, DataEventArgs args)
        {
            byte[] data = args.MyData;
            List<TargetInfo> targetInfos = unPack(data,(Device)sender);
            getComQueue.Enqueue(targetInfos);
        }

        static List<TargetInfo> unPack(byte[] data,Device device)
        {
            List<TargetInfo> targetInfoList = new List<TargetInfo>();
            //数据帧长度由目标辐射源个数
            int num = BitConverter.ToInt16(data.Skip(4).Take(2).ToArray(), 0);
            for (int i = 0; i < num; i++)
            {
                TargetInfo targetInfo = new TargetInfo();
                int headIndex = 6+12*i;
                string targetName = data[headIndex+1].ToString();
                float azimuth = BitConverter.ToInt16(data.Skip(headIndex+2).Take(2).ToArray(), 0);
                float anglePitch = BitConverter.ToInt16(data.Skip(headIndex+4).Take(2).ToArray(), 0);
                float carrier = BitConverter.ToInt16(data.Skip(headIndex + 6).Take(2).ToArray(), 0);
                float range = BitConverter.ToInt16(data.Skip(headIndex + 8).Take(2).ToArray(), 0);
                string targetType = data[headIndex + 10].ToString();
                int error = Convert.ToInt32(data[headIndex + 11]);
                targetInfo.TargetName = targetName;
                targetInfo.Azimuth = (azimuth*0.01).ToString();
                targetInfo.AnglePitch = (anglePitch * 0.01).ToString();
                targetInfo.Carrier = (carrier * 0.1).ToString();
                targetInfo.Range = 
                targetInfo.TargetType = targetType;
                targetInfo.State = error.ToString();
                targetInfo.DeviceName = device.DeviceDto.DeviceName;
                targetInfoList.Add(targetInfo);
            }

            return targetInfoList;
        }
    }

}
